<extend name="Public/common" />
<block name="style">
    <link rel="stylesheet" href="__CSS__/Index/register.css">
</block>
<block name="body">
    <div class="container">
        <form class="form-signin">
            <h2 class="form-signin-heading">用户注册</h2>
            <label for="username" class="sr-only">请输入手机号码</label>
            <input type="tel" id="username" name="username" class="form-control" placeholder="请输入手机号码" required autofocus>
            <label for="password" class="sr-only">Password</label>
            <input type="password" id="password" name="password" class="form-control" placeholder="密码" required>
            <button class="btn btn-lg btn-primary btn-block" type="button" id="registerButton">注册</button>
        </form>
    </div>
</block>
<block name="script">
    <script type="text/javascript">
        $(document).ready(function () {
            $("#registerButton").click(function () {
                register();
            });
        });

        function register() {
            var username = $("#username").val();
            var password = $("#password").val();
            $.ajax({
                type: "post",
                url: "{:U('Home/Index/register')}",
                data: "username=" + username + "&password=" + password,
                success: function (response) {
                    alert(JSON.stringify(response));
                    const code = response.code;
                    if (code === 0) {
                        window.location = "{:U('login')}";
                        //正常执行，成功注册
                    } else {
                        //表明出错,显示错误信息
                        alert(response.message);
                    }
                },
                error: function (response) {
                    alert(JSON.stringify(response));
                }
            });
        }
    </script>
</block>